package crmdna.api.endpoint;

import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.Client;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.programtype.ProgramType;
import crmdna.programtype.ProgramTypeProp;

@Api(name = "programtype")
public class ProgramTypeApi {
	@ApiMethod(path = "create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client,
			@Named("displayName") String displayName,
			@Nullable @Named("practiseIds") Set<Long> practiseIds, User user) {

		try {
			Client.ensureValid(client);
			ProgramTypeProp prop = ProgramType.create(client, displayName,
					practiseIds, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Named("client") String client) {

		try {
			Client.ensureValid(client);
			List<ProgramTypeProp> props = ProgramType.getAll(client);

			return new APIResponse(Status.SUCCESS, props);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "rename", httpMethod = HttpMethod.GET)
	public APIResponse rename(@Named("client") String client,
			@Named("programTypeId") long programTypeId,
			@Named("newDisplayName") String newDisplayName, User user) {

		try {
			Client.ensureValid(client);
			ProgramTypeProp prop = ProgramType.rename(client, programTypeId,
					newDisplayName, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client,
			@Named("programTypeId") long programTypeId, User user) {

		try {
			Client.ensureValid(client);
			ProgramType
					.delete(client, programTypeId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "Program type ["
					+ programTypeId + "] deleted");
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
}
